/************************
 * 
 * @author Justin Adams 
 * Student # : 200735090
 * 
 ***********************/
package tttCore;

//import Board;
//import Move;


/** Board *****************************************************************
 * 
 * Immutable class used to deal with the game board.
 * 
 * ****************************************************************************/
public class Board {

	public char[][] board = new char[3][3];


	/** Default Constructor *****************************************************************
	 * 
	 * Creates a new blank instance of board. Used only once per game for the initial board.
	 * N - NULL ( no piece )
	 * X - X
	 * O - O
	 * @pre		-	
	 * @params	- 
	 * @returns - 
	 * @post	- A new Board object is created and the board member is filled N's
	 * ****************************************************************************/
	public Board(){
		for (int r = 0; r < board.length; r++)
			for (int c = 0; c < board[r].length; c++)
				board[r][c] = 'N';		
	}

	/** Constructor *****************************************************************
	 * 
	 * Creates a board based on the string passed in
	 * N - NULL ( no piece )
	 * X - X
	 * O - O
	 * @pre		- 
	 * @params	- recievedBoard must be in the format r0c0,r0c1,r0c2,r1c0,r1c1,r1c2,r2c0,r2c1,r2c2,
	 * @returns - 
	 * @post	- A new Board object is created and the board member is filled N's
	 * ****************************************************************************/
	public Board(String receivedBoard){
		if(receivedBoard.length() == 9 ){
			char[] charBoard = receivedBoard.toCharArray();
			int count = 0;
			for (int r = 0; r < board.length; r++){
				for (int c = 0; c < board[r].length; c++){
					board[r][c] = charBoard[count];
					count++;
				}
			}
		}
	}
	
	/** Constructor *****************************************************************
	 * 
	 * Creates a board based on the char[] passed in
	 * N - NULL ( no piece )
	 * X - X
	 * O - O
	 * @pre		- char[] recievedBoard must be of size nine
	 * @params	- recievedBoard must be in the format r0c0,r0c1,r0c2,r1c0,r1c1,r1c2,r2c0,r2c1,r2c2,
	 * @returns - 
	 * @post	- A new Board object is created and the board member is filled N's
	 * ****************************************************************************/
	public Board(char[] receivedBoard){
		if(receivedBoard.length == 9 ){
			int count = 0;
			for (int r = 0; r < board.length; r++){
				for (int c = 0; c < board[r].length; c++){
					board[r][c] = receivedBoard[count];
					count++;
				}
			}
		}
	}

	/** Constructor *****************************************************************
	 * 
	 * Creates a new board with the updated position of a performed move.
	 * @pre		-	
	 * @params	- other: The state of the current board.
	 * 			  move: The move made on the board.
	 * @returns - 
	 * @post	- A new board is created with the board member getting the old board
	 * 			  with the additional move added in.
	 * ****************************************************************************/
	public Board(Board other, Move move){
		board = other.board; //Copy the current board.
		board[move.getI()][move.getJ()] = move.getPiece(); //Set the position to the char.
	}

	/** isFull *****************************************************************
	 * 
	 * Tests if all positions are occupied by a piece.
	 * 
	 * @pre		-	
	 * @params	- 
	 * @returns - TRUE if all positions are occupied.
	 * 			  FALSE if there is a single position open (contains 'N')
	 * @post	- 
	 * ****************************************************************************/
	public boolean isFull(){
		for (int r = 0; r < board.length; r++)
			for (int c = 0; c < board[r].length; c++)
				if(board[r][c] == 'N')
					return false;
		return true;

	}

	/** get *****************************************************************
	 * 
	 * returns the piece at the i row j col in the board
	 * 
	 * @pre		-  i and j are less than 3
	 * @params	-  i-row j-col
	 * @returns -  the char at i,j
	 * @post	- 
	 * ****************************************************************************/
	public char get(int i, int j) {
		return board[i][j];
	}
	
	/** printBoardAsString ***********************************************************
	 * 
	 * returns the piece at the i row j col in the board
	 * 
	 * @pre		-  i and j are less than 3
	 * @params	-  
	 * @returns -  the board as a string of length 9
	 * @post	- 
	 * ****************************************************************************/
	public String printBoardAsString(){
		char p;
		String retString = "";
		for (int r = 0; r < board.length; r++)
			for (int c = 0; c < board[r].length; c++){
				p = board[r][c];
				retString = retString.concat(Character.toString(p));
			}
		return retString;
			
	}

}
